/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pbl_servidor;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Asuka
 */
public class trataPing implements Runnable{
    private String dado;
    private Socket s;
    private List<Socket> listaSocketsOnline;
    private PBL_Servidor servidor;
    private InputStream clientein;
    private TelaServidor  telaServidor;
    
    public trataPing(InputStream in,PBL_Servidor p, Socket cli, List<Socket> socketsOnline){
        this.servidor = p;
        this.s = cli;
        this.listaSocketsOnline = socketsOnline;
        this.clientein = in;
    }
    public void run(){
        try {
            DataInputStream lendoDoCliente = new DataInputStream(this.clientein);
            
            System.out.println("Aguardando requisição do cliente");
           telaServidor.exibeMensagem("\nAguardando requisição do cliente");
            String DadosUsuario = lendoDoCliente.readUTF();
            System.out.println("Cliente respondeu - " + DadosUsuario);
           telaServidor.exibeMensagem("\nCliente respondeu - " + DadosUsuario);
            if(DadosUsuario.split(":")[0].equals("PING")){
              //  wait(10000);
                if(DadosUsuario.split(":")[1].equals("OK")){
                System.out.println("Ainda online: "+ s.getInetAddress().getHostAddress());
              //  telaServidor.exibeMensagem("\nAinda online: "+ s.getInetAddress().getHostAddress());
                }
                else{
                    System.out.println("offline: "+ s.getInetAddress().getHostAddress());
                    telaServidor.exibeMensagem("\noffline: "+ s.getInetAddress().getHostAddress());
                }
            }
//        if(dado.split(":")[1].equals("OK")){
//            System.out.println("Ainda online: "+ s.getInetAddress().getHostAddress());
//        }
//        else{
//            System.out.println("offline: "+ s.getInetAddress().getHostAddress());
//        }
        } catch (IOException ex) {
            Logger.getLogger(trataPing.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
